<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  <meta name="author" content="紫锦木">
  

  <title>file_get_contents和curl的速度比较 | 紫锦木</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
  <meta name="keywords" content="PHP,PHP,file_get_contents,curl,">
  

  
  <meta name="description" content="今天我们来比较一下 file_get_contents() 和 curl 方式获取微信头像创建图片耗时分析">

  

  
  <script src="//cdn.jsdelivr.net/npm/leancloud-storage@3.11.1/dist/av-min.js" async></script>
  

  
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
  

  
  <script src="//unpkg.com/valine/dist/Valine.min.js" async></script>
  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"3zwI0gU7grT0MfYUEurqXnWE-gzGzoHsz","appkey":"yVgwglQXfL3vbQiSzeo1Xhqx","comment":true,"count":true},
    welcome: {"enable":false,"interval":30},
    start_time: "2019-05-01",
    passwords: ["efe07af7441da2b69c4a41e42e73be4db47f66010a56900788a458354a7373ec", ],
    is_post: true,
    lock: false,
    author: "紫锦木",
    share: {"twitter":false,"facebook":false,"weibo":true,"qq":true,"wechat":true},
    mathjax: true,
    page_type: "",
    root: "/"
  };
</script>

  <script src="/vendor/sha256.min.js"></script>
<script src="/js/auth.js"></script>
<script src="/js/index.js"></script>
<script src="/vendor/qrcode.min.js"></script>

  
  <link rel="icon" href="/images/favicon.ico">
  <link rel="apple-touch-icon" href="/images/touch-icon.png">
  

  <link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

  <link rel="stylesheet" href="/css/index.css">
<link rel="stylesheet" href="/styles/components/highlight/highlight.css">

  
</head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/">紫锦木</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 足迹👣</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/" target="_self">首页</a>
      
        <a href="/archives/" target="_self">归档</a>
      
        <a href="/tags/" target="_self">标签</a>
      
        <a href="/categories/" target="_self">分类</a>
      
        <a href="/friends/" target="_self">友链</a>
      
        <a href="/about/" target="_self">关于</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://github.com/xfjpeter/" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/" target="_self">
            首页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/friends/" target="_self">
            友链
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/about/" target="_self">
            关于
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2019-12-11
    </span>
    
      <span>
        | <a href="/categories/PHP/"><i class="fa fa-bookmark"></i>PHP</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
  </div>
  <h1 class="passage-title">
    file_get_contents和curl的速度比较
  </h1>
  
  <article class="passage-article">
    <blockquote>
<p>公司最近有一个小项目是需要创建一个分享的海报，上面有用户头像、分享的二维码、还有一个背景海报；其实生成并不难，很快我就搞出来了，搞出来后发现一个问题，就是创建分享海报速度太慢了，我尝试着找原因，后面终于找到了，<code>file_get_contents()</code> 这个方法导致的；下面我们来比较一下用 <code>file_get_contents()</code> 和 <code>curl</code> 方式获取头像，转换成本地图片的耗时</p>
</blockquote>
<p>代码中用到了 vendor 包 <code>composer require johnxu/tool</code></p>
<h2 id="第一种方式-利用-file-get-contents"><a href="#第一种方式-利用-file-get-contents" class="headerlink" title="第一种方式(利用 file_get_contents )"></a>第一种方式(利用 <code>file_get_contents</code> )</h2><blockquote>
<p>具体代码如下</p>
</blockquote>
<iframe src="https://carbon.now.sh/embed?bg=rgba(171%2C184%2C195%2C0)&t=material&wt=none&l=auto&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=0px&ph=0px&ln=false&fl=1&fm=Source%20Code%20Pro&fs=14px&lh=137%25&si=false&es=2x&wm=false&code=%253C%253Fphp%250A%250Ause%2520johnxu%255Ctool%255CHttp%253B%250A%252F%252F%2520%25E5%25BC%2595%25E5%2585%25A5composer%25E8%2587%25AA%25E5%258A%25A8%25E5%258A%25A0%25E8%25BD%25BD%25E7%25B1%25BB%250Arequire%2520'.%252Fvendor%252Fautoload.php'%253B%250A%250A%252F%252F%2520%25E5%25AE%259A%25E4%25B9%2589%25E8%258E%25B7%25E5%258F%2596%25E6%2597%25B6%25E9%2597%25B4%25E7%259A%2584%25E5%2587%25BD%25E6%2595%25B0%250Afunction%2520microtime_float()%250A%257B%250A%2520%2520%2520%2520%255B%2524usec%252C%2520%2524sec%255D%2520%253D%2520explode('%2520'%252C%2520microtime())%253B%250A%250A%2520%2520%2520%2520return%2520((float)%2520%2524usec%2520%252B%2520(float)%2520%2524sec)%253B%250A%257D%250A%252F%252F%2520%25E5%25AE%259A%25E6%2597%25B6%25E5%25BC%2580%25E5%25A7%258B%25E6%2597%25B6%25E9%2597%25B4%250A%2524start%2520%253D%2520microtime_float()%253B%250A%250A%2524avatar%2520%253D%2520'http%253A%252F%252Fthirdwx.qlogo.cn%252Fmmopen%252Fvi_32%252FY9Q8FlPyicLtAiaP5FHOnheBtgxjOHsqO5bvHXg5ia2tP3ocaQAHnhLBHDHzibqP78UqsYr4plnmxjn3JynjXKHImg%252F132'%253B%250A%252F%252F%2520%25E4%25B8%258B%25E9%259D%25A2%25E4%25B8%2580%25E8%25A1%258C%250A%2524img%2520%253D%2520imagecreatefromstring(file_get_contents(%2524avatar))%253B%250A%250A%252F%252F%2520%25E5%25AE%259A%25E4%25B9%2589%25E7%25BB%2593%25E6%259D%259F%25E6%2597%25B6%25E9%2597%25B4%250A%2524end%2520%253D%2520microtime_float()%253B%250A%252F%252F%2520%25E8%25BE%2593%25E5%2587%25BA%25E8%2580%2597%25E6%2597%25B6%250Avar_dump('%25E8%2580%2597%25E6%2597%25B6%25EF%25BC%259A'%2520.%2520sprintf('%252510f'%252C%2520(%2524end%2520-%2520%2524start))%2520.%2520'%25E7%25A7%2592.')%253B" style="transform:scale(1); width:1024px; height:580px; border:0; overflow:hidden;" sandbox="allow-scripts allow-same-origin"><br></iframe>

<p>我在 PHP72 上跑的，耗时差不多：18.557844 秒</p>
<h2 id="第二种方式-利用-curl-这种方式"><a href="#第二种方式-利用-curl-这种方式" class="headerlink" title="第二种方式( 利用 curl 这种方式 )"></a>第二种方式( 利用 <code>curl</code> 这种方式 )</h2><blockquote>
<p>具体代码如下</p>
</blockquote>
<iframe src="https://carbon.now.sh/embed?bg=rgba(171%2C184%2C195%2C0)&t=material&wt=none&l=text%2Fx-php&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=0px&ph=0px&ln=false&fl=1&fm=Source%20Code%20Pro&fs=13px&lh=161%25&si=false&es=1x&wm=false&code=%253C%253Fphp%250A%250Ause%2520johnxu%255Ctool%255CHttp%253B%250A%252F%252F%2520%25E5%25BC%2595%25E5%2585%25A5composer%25E8%2587%25AA%25E5%258A%25A8%25E5%258A%25A0%25E8%25BD%25BD%25E7%25B1%25BB%250Arequire%2520'.%252Fvendor%252Fautoload.php'%253B%250A%250A%252F%252F%2520%25E5%25AE%259A%25E4%25B9%2589%25E8%258E%25B7%25E5%258F%2596%25E6%2597%25B6%25E9%2597%25B4%25E7%259A%2584%25E5%2587%25BD%25E6%2595%25B0%250Afunction%2520microtime_float()%250A%257B%250A%2520%2520%2520%2520%255B%2524usec%252C%2520%2524sec%255D%2520%253D%2520explode('%2520'%252C%2520microtime())%253B%250A%250A%2520%2520%2520%2520return%2520((float)%2520%2524usec%2520%252B%2520(float)%2520%2524sec)%253B%250A%257D%250A%252F%252F%2520%25E5%25AE%259A%25E6%2597%25B6%25E5%25BC%2580%25E5%25A7%258B%25E6%2597%25B6%25E9%2597%25B4%250A%2524start%2520%253D%2520microtime_float()%253B%250A%250A%2524avatar%2520%253D%2520'http%253A%252F%252Fthirdwx.qlogo.cn%252Fmmopen%252Fvi_32%252FY9Q8FlPyicLtAiaP5FHOnheBtgxjOHsqO5bvHXg5ia2tP3ocaQAHnhLBHDHzibqP78UqsYr4plnmxjn3JynjXKHImg%252F132'%253B%250A%252F%252F%2520%25E4%25B8%258B%25E9%259D%25A24%25E8%25A1%258C%250A%2524result%2520%253D%2520Http%253A%253AgetInstance()-%253Erequest()%253B%250Aif%2520(%2524result-%253EgetCode()%2520%253D%253D%2520200)%2520%257B%250A%2520%2520%2520%2520%2524img%2520%253D%2520imagecreatefromstring(%2524result-%253Eget('data'))%253B%250A%257D%250A%250A%252F%252F%2520%25E5%25AE%259A%25E4%25B9%2589%25E7%25BB%2593%25E6%259D%259F%25E6%2597%25B6%25E9%2597%25B4%250A%2524end%2520%253D%2520microtime_float()%253B%250A%252F%252F%2520%25E8%25BE%2593%25E5%2587%25BA%25E8%2580%2597%25E6%2597%25B6%250Avar_dump('%25E8%2580%2597%25E6%2597%25B6%25EF%25BC%259A'%2520.%2520sprintf('%252510f'%252C%2520(%2524end%2520-%2520%2524start))%2520.%2520'%25E7%25A7%2592.')%253B" style="transform:scale(1); width:1024px; height:652px; border:0; overflow:hidden;" sandbox="allow-scripts allow-same-origin"><br></iframe>

<p>我在 PHP72 上跑的，耗时差不多：0.114182 秒</p>
<blockquote>
<p>总结一下：这两种方式下耗时相差十万八千里，大概 200 倍左右吧，刚开始的为了图码代码快一点，用的<code>file_get_contents()</code>。所以说码代码快一点还是要考虑性能的，性能快才是王道，码代码只是辅助而已。</p>
</blockquote>
<p>关键相差的代码如下：</p>
<iframe src="https://carbon.now.sh/embed?bg=rgba(171%2C184%2C195%2C0)&t=material&wt=none&l=text%2Fx-php&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=0px&ph=0px&ln=false&fl=1&fm=Source%20Code%20Pro&fs=13px&lh=138%25&si=false&es=2x&wm=false&code=%252F%252F%2520file_get_contents()%25E6%2596%25B9%25E5%25BC%258F%250A%2524img%2520%253D%2520imagecreatefromstring(file_get_contents(%2524avatar))%253B%250A%250A%252F%252F%2520curl%25E6%2596%25B9%25E5%25BC%258F%250A%2524result%2520%253D%2520Http%253A%253AgetInstance()-%253Erequest()%253B%250Aif%2520(%2524result-%253EgetCode()%2520%253D%253D%2520200)%2520%257B%250A%2520%2520%2520%2520%2524img%2520%253D%2520imagecreatefromstring(%2524result-%253Eget('data'))%253B%250A%257D" style="transform:scale(1); width:1024px; height:473px; border:0; overflow:hidden;" sandbox="allow-scripts allow-same-origin"><br></iframe>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#第一种方式-利用-file-get-contents"><span class="toc-text">第一种方式(利用 file_get_contents )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#第二种方式-利用-curl-这种方式"><span class="toc-text">第二种方式( 利用 curl 这种方式 )</span></a></li></ol>
  </div>
</aside>
  
    <aside class="passage-copyright">
      <div>本文作者: 紫锦木</div>
      
        <div>
          原文链接: 
          <a href target="_blank">https://www.johnxu.net/articles/ck9jyogk4000age8o3yr1aum8.html</a>
        </div>
      
      <div>
        版权声明: 本博客所有文章除特别声明外, 均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议. 转载请注明出处!
      </div>
    </aside>
  
  
    <div class="passage-tags">
     
      <a href="/tags/PHP/"><i class="fa fa-tags"></i>PHP</a>
     
      <a href="/tags/file-get-contents/"><i class="fa fa-tags"></i>file_get_contents</a>
     
      <a href="/tags/curl/"><i class="fa fa-tags"></i>curl</a>
    
    </div>
  
</div>

    </main>
    
      
<div class="site-comment-contanier" data-plateform="leancloud">
  
    <p id="site-comment-info">
      <i class="fa fa-spinner fa-spin"></i> 评论加载中
    </p>
    <div id="site-comment"></div>
  
</div>
    
    <div class="site-footer-wrapper">
  <footer class="site-footer">
    
    
    <div class="site-footer-col">
      <h5 class="site-footer-title">博客推荐</h5>
      
      <span class="site-footer-item">
        <a href="https://godbmw.com/" target="_blank">GodBMW</a>
      </span>
      
      <span class="site-footer-item">
        <a href="http://ruanyifeng.com/" target="_blank">阮一峰的个人网站</a>
      </span>
      
    </div>
    
    <div class="site-footer-col">
      <h5 class="site-footer-title">抓到我</h5>
      
      <span class="site-footer-item">
        <a href="https://juejin.im/user/5b4ffe335188251afa62cd8a" target="_blank">掘金</a>
      </span>
      
      <span class="site-footer-item">
        <a href="https://segmentfault.com/u/johnxu_58f42b627331b" target="_blank">思否</a>
      </span>
      
      <span class="site-footer-item">
        <a href="https://www.jianshu.com/u/56f6d0e3bae3" target="_blank">简书</a>
      </span>
      
    </div>
    
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
    <div class="site-footer-info">
      <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
    </div>
    
    
    <div class="site-footer-info">
      <i class="fa fa-at"></i> Email: fsyzxz@163.com
    </div>
    
    <div class="site-footer-info">
      <i class="fa fa-copyright"></i>
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div>
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">
  <div class="bottom-bar-left">
    <a href="/articles/ck9jyogke000rge8ovqnoo8sm.html" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/articles/ck9jyogkc000pge8ojoqtbzev.html" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
      <a href="javascript:void(0);" id="site-reward">
        <i class="fa fa-thumbs-up"></i>
      </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
    <a id="share-btn-weibo" href="javascript:void(0);" target="_blank">
      <i class="fa fa-weibo"></i>
    </a>
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
    <a id="share-btn-wechat" href="javascript:void(0);" target="_blank">
      <i class="fa fa-wechat"></i>
    </a>
  
</div>
    


  <script async>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>




    
  </body>
</html>